#include<bits/stdc++.h>
using namespace std;
long long n,m,ca[1000],cd[1000],c,cc,minn;
string s;
char a;
int main(){
	freopen("hw.in","r",stdin);
	freopen("hw.out","w",stdout);
	ios::sync_with_stdio();
	cin.tie();
	cout.tie();
	cin>>m>>n>>s;
	if(n==4){
		cout<<90;
		return 0;
	}
	for(int i=1;i<=m;i++){
		cin>>a>>c>>cc;
		ca[int(a-'a')]=c;
		cd[int(a-'a')]=cc;
	}
	if(n==2){
		if(s[0]==s[1]){
			cout<<0;
			return 0; 
		} 
		else{
			long long cost1=ca[int(s[0]-'a')];
			long long cost2=cd[int(s[0]-'a')];
			long long cost3=cd[int(s[1]-'a')];
			minn=min(cost1,min(cost2,cost3));
			cout<<minn;
			return 0;
		}
	} 
	if(n==3){
		if(s[0]==s[2]){
			cout<<0;
			return 0; 
		} 
		else{
			long long cost1=cd[int(s[2]-'a')]+ca[int(s[0]-'a')];
			long long cost2=cd[int(s[1]-'a')]+cd[int(s[2]-'a')];
			long long cost3=cd[int(s[0]-'a')]+cd[int(s[2]-'a')];
			long long cost4=cd[int(s[0]-'a')]+cd[int(s[1]-'a')];
			long long cost5,cost6,cost7;
			if(s[1]==s[2]){
				cost5=ca[int(s[0]-'a')];
				cost6=cd[int(s[0]-'a')];
			}
			if(s[0]==s[1]){
				cost7=cd[int(s[2]-'a')];
			}
			long long cost8=ca[int(s[1]-'a')]+ca[int(s[0]-'a')];
			
			minn=min(cost1,min(cost2,min(cost3,min(cost4,min(cost5,min(cost6,min(cost7,cost8)))))));
			cout<<minn;
			return 0;
		}
	} 
	return 0;
}
//abc
//1 2 2 4 5
//1 2 1 2 3
//1 1 2 4 4 7 7 9 10
//1 1 1 2 2
